package ext.tianma.part.finishedProduct.validators;

import java.util.Locale;

import wt.fc.Persistable;
import wt.fc.WTReference;
import wt.org.WTGroup;
import wt.org.WTPrincipal;
import wt.part.WTPart;
import wt.session.SessionHelper;
import wt.util.WTException;

import com.ptc.core.ui.validation.DefaultUIComponentValidator;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationStatus;

import ext.tianma.bom.util.GenericUtil;
import ext.workflow.WorkFlowUtil;

/**
 * 成品流程入口权限，项目经理有权限
 * @author Administrator
 *
 */
//ext.tianma.part.finishedProduct.validators.ReleaseFinishedGoodValidator
public class ReleaseFinishedGoodValidator extends DefaultUIComponentValidator {

	@Override
	public UIValidationStatus preValidateAction(UIValidationKey uivalidationkey,UIValidationCriteria uivalidationcriteria) {
		UIValidationStatus uivalidationstatus = UIValidationStatus.DISABLED;
		try {
			WTReference wtreference = uivalidationcriteria.getContextObject();
            Persistable persistable = wtreference.getObject();
			
			if(persistable != null && persistable instanceof WTPart) {
			    WTPart wtpart = (WTPart) persistable;
			    String partstate = wtpart.getState().getState().getDisplay(Locale.CHINA);
			    if (!"正在工作".equals(partstate)){
			    	return uivalidationstatus;
				}
			}
			
			if(GenericUtil.isAdministrators() || GenericUtil.isOrgAdmin()){
				return UIValidationStatus.ENABLED;
			}
			
			String pmGroupName = "Asi-QS(项目管理组)";
			WTPrincipal principal = SessionHelper.manager.getPrincipal();
			WTGroup pmGroup = WorkFlowUtil.getGroup(pmGroupName);
			if(pmGroup.isMember(principal)) {
			   uivalidationstatus =  UIValidationStatus.ENABLED;
		    }
		} catch (WTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return uivalidationstatus;
	}
}